package thread;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-05-10
 * Time：22:16
 */
public class Demo6 {
    private static final long COUNT = 10_0000_0000;
    private static void serial() {
        long begin = System.currentTimeMillis();

        int a = 0;
        for (int i = 0; i < COUNT; i++) {
            a++;
        }
        a = 0;
        for (int i = 0; i < COUNT; i++) {
            a++;
        }
        long end = System.currentTimeMillis();
        System.out.println("共花费了" + (end - begin) + "ms");
    }

    public static void main(String[] args) {
        concurrency();
    }

    private static void concurrency() {
        long begin = System.currentTimeMillis();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < COUNT; i++) {
                int a = 0;
                a++;
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < COUNT; i++) {
                int a = 0;
                a++;
            }
        });
        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println("共花费了" + (end - begin) + "ms");
    }
}

